Certified Java Programmer Mock Exam


Question 1


abstract class A {
  private int x = 4;
  private int y = 2;
  public int x() {return x;}
  public void x(int x) {this.x = x;}
  public int y() {return y;}
  public void y(int y) {this.y = y;}
  public abstract int math();
}
class B {
  static A a1 = new A(2,1) {
    public A(int i1, int i2) {x(i1);y(i2);};
    public int math() {return x()+y();}
  };
  public static void main(String[] args) {
    System.out.print(a1.math());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 8
b. Prints: 3122
c. Compile-time error.
d. Run-time error.
e. None of the Above.

Question 2


abstract class A {            // 1
  abstract final void m1();   // 2
  abstract final class B {}   // 3
  class C extends B {}        // 4
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 3



Which of the following are class modifiers? Select all that are applicable to a top-level class and all that are applicable to a nested class. It is not required that a modifier be applicable to both.

a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. static
i. synchronized
j. transient
k. volatile
l. strictfp
m. None of the above.

Question 4



Which of the follow are true statements.

a. A nested class is any class that is declared within the body of another class or interface.
b. A nested class can not be declared within the body of an interface declaration.
c. A top level class is a class this is not a nested class.
d. An inner class is a nested class that is not static.
e. A nested class can not be declared static.
f. A named class is any class that is not anonymous.
g. None of the above.

Question 5



Which of the following modifiers can be applied to a member class?

a. public
b. protected
c. private
d. abstract
e. static
f. final
g. strictfp
h. None of the above.

Question 6



Which of the follow are true statements.

a. An anonymous class can be declared abstract.
b. An incompletely implemented class must be declared abstract.
c. A local class can be declared abstract.
d. An abstract class can be instantiated.
e. An abstact class is implicitly final.
f. An abstact class must declare at least one abstract method.
g. An abstact class can not extend a concrete class.
h. None of the above.

Question 7


abstract class A {               // 1
  private abstract void m1();    // 2
  private abstract class B {}    // 3
  private class C extends B {}   // 4
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. None of the Above.

Question 8


class A {
  A() {}        // 1
  int A;        // 2
  void A() {}   // 3
  class A {}    // 4
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 9



If the strictfp modifier is applied to a class, then all float or double expressions in which of the following will also be implicitly strictfp?

a. nested classes
b. methods
c. constructors
d. static initializers
e. instance initializers
f. variable initializers
g. None of the above.

Question 10


abstract class A {               // 1
  abstract strictfp void m1();   // 2
  abstract strictfp class B {}   // 3
  strictfp class C extends B {}  // 4
}

Compiler errors are generated at which of the following lines?

a. 1
b. 2
c. 3
d. 4
e. None of the Above

Question 11


class Z {
  abstract class A {}     // 1
  final class B {}        // 2
  private class C {}      // 3
  protected class D {}    // 4
  public class E {}       // 5
  static class F {}       // 6
  synchronized class G {} // 7
  transient class H {}    // 8
  volatile class I {}     // 9
  strictfp class J {}     // 10
}

Which of the class declarations results in a compiler error?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. 10
k. None of the Above

Question 12


class A {
  private static String s1 = "s1";
  final String s2 = "s2";
  A () { new Z("s5","s6");}
  class Z {
    final String s3 = "s3";
    String s4 = "s4";
    Z (final String s5, String s6) {
      System.out.print(???);
    }
  }
  public static void main(String args[]) {new A();}
}

Which variables can be substituted for ??? without causing a compiler error?

a. s1 
b. s2 
c. s3 
d. s4 
e. s5 
f. s6 
g. Runtime error.
h. Compiler error.
i. None of the Above

Question 13


class B {
  private static String s1 = "s1";
  final String s2 = "s2";
  B () { new Z("s5","s6");}
  static class Z {
    final String s3 = "s3";
    static String s4 = "s4";
    Z (final String s5, String s6) {
      System.out.print(???);
    }
  }
  public static void main(String args[]) {new B();}
}

Which variables can be substituted for ??? without causing a compiler error?

a. s1 
b. s2 
c. s3 
d. s4 
e. s5 
f. s6 
g. Runtime error
h. Compiler error
i. None of the above

Question 14


1.  class Outer {
2.    static class StaticNested {
3.      static final int a = 25;
4.      static final int b;
5.      static int c;
6.      int d;
7.      static {b = 42;}
8.    }
9.    class NonStaticInner {
10.     static final int e = 25;
11.     static final int f;
12.     static int g;
13.     int h;
14.     static {f = 42;}
15.   }
16. }

What is the result of attempting to compile the program?

a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. Compiler error at line 9.
h. Compiler error at line 10.
i. Compiler error at line 11.
j. Compiler error at line 12.
k. Compiler error at line 13.
l. Compiler error at line 14.
m. None of the Above

Question 15


1.  class Red {
2.    static class StaticNested {
3.      interface ABC {}
4.    }
5.    class NonStaticInner {
6.      interface DEF {}
7.    }
8.    interface GHI {}
9.  }

What is the result of attempting to compile the program?

a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 8.
f. None of the Above

Question 16


class G {
  final String s1 = "G.s1";
  class Z {
    String s1;
    void m1() {System.out.println(???);}
  }
  public static void main(String args[]) {
    G g = new G();
    g.new Z().m1();
  }
}

What field access expression could be used in place of ??? above to cause the program to print "G.s1"?

a. s1
b. G.s1
c. ((G)this).s1
d. G.this.s1
e. G.super.s1
f. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1